﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace August.UI
{
    public class UserControlX : UserControl
    {
        public float InitX;
        public float InitY;
        public float InitWidth;
        public float InitHeight;

        private float m_ScaleX = 1F;
        private float m_ScaleY = 1F;

        private float m_OffsetX = 0F;
        private float m_OffsetY = 0F;

        public virtual float ScaleX
        {
            get
            {
                return m_ScaleX;
            }
            set
            {
                m_ScaleX = value;
            }
        }

        public virtual float ScaleY
        {
            get
            {
                return m_ScaleY;
            }
            set
            {
                m_ScaleY = value;
            }
        }

        public virtual float OffsetX
        {
            get
            {
                return m_OffsetX;
            }
            set
            {
                m_OffsetX = value;
            }
        }
        public virtual float OffsetY
        {
            get
            {
                return m_OffsetY;
            }
            set
            {
                m_OffsetY = value;
            }
        }
        public UserControlX()
        {
            //this.SetStyle(ControlStyles.Opaque, true);

            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
           // Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
           //| System.Windows.Forms.AnchorStyles.Left)
           //| System.Windows.Forms.AnchorStyles.Right)));
            InitHeight = Height;
            InitWidth = Width;
            InitX = Location.X;
            InitY = Location.Y;
        }

        ShapeBaseCollection shapeBaseitems;
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [Localizable(false)]
        public ShapeBaseCollection ShapeItems
        {
            get
            {
                if (shapeBaseitems == null)
                {
                    shapeBaseitems = new ShapeBaseCollection(this);
                }
                return shapeBaseitems;
            }
        }


        ImageBaseCollection imageBaseitems;
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [Localizable(false)]
        public ImageBaseCollection ImageItems
        {
            get
            {
                if (imageBaseitems == null)
                {
                    imageBaseitems = new ImageBaseCollection(this);
                }
                return imageBaseitems;
            }
        }



        //protected override CreateParams CreateParams
        //{
        //    get
        //    {
        //        CreateParams cp = base.CreateParams;
        //        cp.ExStyle = 0x20;
        //        return cp;
        //    }
        //}

        protected unsafe Region GetRegion(Bitmap bckImage)
        {
            GraphicsPath path = new GraphicsPath();
            int w = bckImage.Width;
            int h = bckImage.Height;
            BitmapData bckdata = null;
            try
            {
                bckdata = bckImage.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
                uint* bckInt = (uint*)bckdata.Scan0;
                for (int j = 0; j < h; j++)
                {
                    for (int i = 0; i < w; i++)
                    {
                        if ((*bckInt & 0xff000000) != 0)
                        {
                            path.AddRectangle(new Rectangle(i, j, 1, 1));
                        }
                        bckInt++;
                    }
                }
                bckImage.UnlockBits(bckdata);
                bckdata = null;
            }
            catch
            {
                if (bckdata != null)
                {
                    bckImage.UnlockBits(bckdata);
                    bckdata = null;
                }
            }
            Region region = new System.Drawing.Region(path);
            path.Dispose();
            path = null;
            return region;
        }


    }
}
